package com.woniuxy.propertyjava.controller;

import com.woniuxy.propertyjava.entity.Equipment;
import com.woniuxy.propertyjava.entity.PageBean;
import com.woniuxy.propertyjava.entity.ResponseResult;
import com.woniuxy.propertyjava.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : author
 * @create 2022/7/5 10:21
 */
@RestController
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;


    @PostMapping("/equipment/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Equipment>> getEquipmentsByPage(@PathVariable("pageIndex") int pageIndex,
                                                                   @PathVariable("pageSize") int pageSize,
                                                                   @RequestBody Equipment equipment) {
        PageBean<Equipment> pageBean = equipmentService.getEquipmentPageBean(equipment, pageIndex, pageSize);
        ResponseResult<PageBean<Equipment>> responseResult = new ResponseResult<PageBean<Equipment>>(200, "ok", pageBean);
        return responseResult;
    }

    @PostMapping("/equipment")
    public ResponseResult addEquipment(@RequestBody Equipment equipment) {
        equipmentService.insertEquipment(equipment);
        ResponseResult responseResult = new ResponseResult<>(200, "添加成功", null);
        return responseResult;
    }

    @PutMapping("/equipment")
    public ResponseResult changeSEquipment(@RequestBody Equipment equipment) {
        equipmentService.updataEquipment(equipment);
        ResponseResult responseResult = new ResponseResult<>(200, "修改成功", null);
        return responseResult;
    }

    @DeleteMapping("/equipment/{equipmentId}")
    public ResponseResult reduceEquipment(@PathVariable("equipmentId") int equipmentId) {
        equipmentService.deleteEquipment(equipmentId);
        ResponseResult responseResult = new ResponseResult<>(200, "删除成功", null);
        return responseResult;
    }
}
